//
//  CallNotesTranslatorsNumView.swift
//  GLUZSwift
//
//  Created by xyanl on 2025/3/17.
//  Copyright © 2025 考拉🐨. All rights reserved.
//

import UIKit
import Reusable

extension CallNotesTranslatorsNumView: NibLoadable {}

protocol CallNotesTranslatorsNumViewDelegate: AnyObject {
    func didSelectedItem(model: CallNotesTranslatorModel)
}

class CallNotesTranslatorsNumView: UIView {
    
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var collectionLayoutHeight: NSLayoutConstraint!
    
    weak var delegate: CallNotesTranslatorsNumViewDelegate?
    
    var list: [CallNotesTranslatorModel] = [] {
        didSet {
            updateCollectionView()
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        initUI()
    }
    
}

private extension CallNotesTranslatorsNumView {
    
    func initUI() {
        backgroundColor = UIColor(hex: "F7F7F7")
        
        let layout = YLCollectionViewFlowLayout()
        layout.delegate = self
        layout.columnSpacing = 12
        layout.rowSpacing = 12
        
        collectionView.setCollectionViewLayout(layout, animated: true)
        collectionView.delegate = self
        collectionView.dataSource = self
        
        collectionView.isScrollEnabled = false
        collectionView.bounces = false
        collectionView.backgroundColor = UIColor(hex: "F7F7F7")
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.showsVerticalScrollIndicator = false
        collectionView.register(cellType: CallNotesTranslationCountItemCell.self)
    }
    
    func updateCollectionView() {
        collectionView.reloadData()
        collectionView.layoutIfNeeded()
        collectionLayoutHeight.constant = collectionView.contentSize.height

    }
    
}

extension CallNotesTranslatorsNumView: UICollectionViewDelegate, UICollectionViewDataSource, YLCollectionFlowLayoutDelegate {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        list.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(for: indexPath, cellType: CallNotesTranslationCountItemCell.self)
        let model = list[indexPath.row]
        cell.titleLabel.text = LanguageManager.currentLanguageToText(model.name, model.nameRu) 
        cell.addRoundedCorners(radius: 8)
        cell.contentView.backgroundColor = model.isSelected ? UIColor.themeColor: UIColor(hex: "F3f3f6")
        cell.titleLabel.textColor = model.isSelected ? UIColor.white: UIColor(hex: "#666666")
        return cell
    }
    
    func collectionView(sizeForItemAt index: Int) -> CGSize {
        let model = list[index]
        let name = LanguageManager.currentLanguageToText(model.name, model.nameRu) 
        var width = name.sg.size(width: CGFLOAT_MAX, font: .systemFont(ofSize: 10)).width
        
        width += CGFloat(20)
        width = ceil(width)
        width = min(xScreenW - 16*2, width)
        return CGSizeMake(width, 32)
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        list.forEach { model in
            model.isSelected = false
        }
        
        let model = list[indexPath.row]
        model.isSelected = true
        collectionView.reloadData()
        delegate?.didSelectedItem(model: model)
    }
}
